package com.truckdispatching.image;

import java.io.File;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;

public class ImageFileLoadTask extends ImageTask {

	public ImageFileLoadTask(ImageTask task) {
		super(task);
	}

	public ImageFileLoadTask(File saveFile, String imageUrl) {
		super(saveFile, imageUrl);
	}

	@Override
	public void run() {
		if (isCancel) {
			return;
		}
		if (mListener != null) {
			if (mImageUrl != null && !mImageUrl.equals("") && mImageFile != null && mImageFile.exists()) {
				Options o = new Options();
				o.inJustDecodeBounds = true;
				o.inPreferredConfig = Bitmap.Config.RGB_565;
				Bitmap bitmap = BitmapFactory.decodeFile(mImageFile.getAbsolutePath(), o);
				int width = o.outWidth;
				Options o1 = new Options();
				o1.inPreferredConfig = Bitmap.Config.RGB_565;
				int scale = width / 160;
				if (scale > 1) {
					o1.inSampleSize = scale;
				}
				bitmap = BitmapFactory.decodeFile(mImageFile.getAbsolutePath(), o1);
				if (bitmap != null) {
					mListener.success(this, bitmap);
				} else {
					mImageFile.delete();
					mListener.fail(this);
				}
			} else {
				mListener.fail(this);
			}
		}
	}
}
